var text = '';
var issetbtn = false;
var issetselect = false;
var selectVal = 'en';
var toTop = 0;
var scrollDiv = null;
var issetscroll = false;
var timer = () => {
    return setInterval(() => {
    var msgpop = document.querySelectorAll('.xyPiWz6JGA');
    if (msgpop.length > 0) {
        if (msgpop[msgpop.length - 1].childNodes.length == 1) {
            text = msgpop[msgpop.length - 1].childNodes[0].innerText;
            window.postMessage({type:'receive',text:text,language:selectVal}, '*');
        }
    }

    //插入按钮
    var button = document.querySelector('._1IwhRgTPYe');
    if (issetbtn == false && button != null) {
        var div = document.createElement('div');
        div.innerHTML = '<button id=\"newBtn\" style=\"position:absolute;right:60px;bottom:15px;width:80px;font-size:10px;\">翻译并发送</button>';
        button.appendChild(div);
        issetbtn = true;
        document.querySelector('#newBtn').addEventListener('click',() =>{
            let input = document.querySelector('._1x2tTFFJ3K');
            let text = input.value;
            if (text != "") {
                window.postMessage({type:'send',text:text,language:selectVal}, '*')
            }
        })
    }

    //插入下拉框
    var chatTitle = document.querySelector('._Re39xU1xW');
    if (issetselect == false && chatTitle != null) {
        var div = document.createElement('div');
        div.innerHTML = `
        <select id=\"select\" style=\"position:absolute;right:470px;top:105px;outline:none;\">
            <option value=\"en\" id=\"options\">英语</option>
            <option value=\"th\" id=\"options\">泰语</option>
            <option value=\"ms\" id=\"options\">马来语</option>
            <option value=\"id\" id=\"options\">印尼语</option>
        </select>
        `;
        chatTitle.appendChild(div);
        issetselect = true;
        document.querySelector('#select').addEventListener('change',() =>{
            selectVal = document.querySelector('#select').value;
        });
    }
    if (chatTitle == null) {
        issetselect = false;
    }

    //监听滚动
    scrollDiv = document.querySelector('._3O7HHuyggE');



}, 500);
}
timer();
// setInterval(() => {
//     if (scrollDiv != null && !issetscroll) {
//         issetscroll = true;
//         scrollDiv.addEventListener('scroll',() => {
//             clearInterval(timer)
//             console.log('clear')
//             console.log("滚动高度",scrollDiv.scrollTop)
//             toTop = scrollDiv.scrollTop;
//             setTimeout(() => {
//                 console.log('timeout',scrollDiv.scrollTop,toTop)
//                 if (scrollDiv.scrollTop == toTop) {
//                     timer()
//                     console.log('xiangtong')
//                 }
//             }, 1000);
//         })
//     }
// }, 100);




